//
//  ViolationCarCell.m
//  carcareIOS
//
//  Created by ileo on 16/8/5.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "ViolationCarCell.h"
#import "NSString+CGSize.h"
#define CELL_HEIGHT (75)

#define Label_Color (COLOR_HEXSTRING(@"#2e2e2e"))
#define Label_Gray_Color (COLOR_HEXSTRING(@"#9c9c9c"))
#define Label_Red_Color (COLOR_HEXSTRING(@"#fd6500"))

@interface ViolationCarCell()

@property (nonatomic , strong) UIImageView * iconImageV;

@property (nonatomic , strong) UILabel * titleL;

@property (nonatomic , strong) UILabel * grayTitleL;//车牌右侧待边框的label

@property (nonatomic , strong) UIView * bottomWaningV;//带有警示图片的提示

@property (nonatomic , strong) UILabel * warningDetailL;//警示图片右侧label

@property (nonatomic , strong) UIView * bottomeLabelV;//带有三个label 的 违章各项分数的label
@property (nonatomic , strong) UILabel * breakRulesL;//违章

@property (nonatomic , strong) UILabel * fineMoneyL;//罚款

@property (nonatomic , strong) UILabel * scroreL;//扣分
@end

@implementation ViolationCarCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
    }
    return self;
}
- (void)createUI
{
    CGFloat iconX = 9 * __MAIN_RATIO_375;
    UIImage * icon = ImageNamed(@"BRCarStyleNormalImage");
    CGFloat iconImageVW = 107 * __MAIN_RATIO_375 ;
    CGFloat iconImageVH = iconImageVW * icon.size.height / icon.size.width;
    self.iconImageV = [[UIImageView imageViewWithFrame:CGRectMake(iconX, (CELL_HEIGHT - iconImageVH) / 2, iconImageVW, iconImageVH) image:icon] setupOnView:self];
    
    
    UIImage * rightImage = ImageNamed(@"cell_right");
    CGFloat rightIVX = __MAIN_WIDTH - 15 * __MAIN_RATIO_375 - rightImage.size.width / 2 ;
    self.rightImageV = [[UIImageView imageViewWithCenter:CGPointMake(rightIVX , CELL_HEIGHT / 2) image:rightImage] setupOnView:self];
    
    CGFloat titleLX = self.iconImageV.right + 7 * __MAIN_RATIO_375;
    CGFloat titleLW = 110;
    CGFloat titleLY = 13;
    CGFloat titleLH = 21;
    self.titleL = [[UILabel labelWithFrame:CGRectMake(titleLX, titleLY, titleLW, titleLH) font:FONT_DEFAULT_Bold(16) text:@"" textColor:Label_Color] setupOnView:self];
    self.titleL.backgroundColor = [UIColor clearColor];
    
    CGFloat grayTitleLH = 18;
    CGFloat grayTitleLW = 52;
    self.grayTitleL = [[[[UILabel labelWithFrame:CGRectMake(self.titleL.right, (titleLH - grayTitleLH) / 2 + self.titleL.top, grayTitleLW, grayTitleLH) font:FONT_DEFAULT_Light(11) text:@"绑定车辆" textColor:COLOR_HEXSTRING(@"#9c9c9c")] setupOnView:self]resetBorderWidth:1 borderColor:[Theme lineColor]] resetCornerRadius:2];
    
    self.bottomWaningV = [[UIView viewWithFrame:CGRectMake(self.titleL.left, CELL_HEIGHT - titleLY - titleLH, __MAIN_WIDTH - self.titleL.left - (__MAIN_WIDTH - self.rightImageV.left), titleLH)] setupOnView:self];
    self.bottomWaningV.hidden = YES;
    
    UIImage * warningImage = ImageNamed(@"myCarWarningImage");
    UIImageView * warningImageV = [[UIImageView imageViewWithCenter:CGPointMake(warningImage.size.width / 2, self.bottomWaningV.height / 2) image:warningImage] setupOnView:self.bottomWaningV];
    
    CGFloat warningLX = warningImageV.right + 5;
    self.warningDetailL = [[UILabel labelWithFrame:CGRectMake(warningLX, 0, self.bottomWaningV.width - warningLX, self.bottomWaningV.height) font:FONT_DEFAULT_Light(13) text:@"" textColor:COLOR_HEXSTRING(@"#fd6500")] setupOnView:self.bottomWaningV];
    
    self.bottomeLabelV = [[UIView viewWithFrame:self.bottomWaningV.frame] setupOnView:self];
    self.bottomeLabelV.hidden = YES;
    
    CGFloat threeLW = self.bottomeLabelV.width / 3 + 10;
    self.breakRulesL = [[UILabel labelWithFrame:CGRectMake(0, 0, threeLW , self.bottomeLabelV.height) font:FONT_DEFAULT_Light(13) text:@"" textColor:Label_Color] setupOnView:self.bottomeLabelV];
    self.fineMoneyL = [[UILabel labelWithFrame:CGRectMake(self.breakRulesL.right, self.breakRulesL.top, self.breakRulesL.width , self.breakRulesL.height) font:self.breakRulesL.font text:@"" textColor:Label_Color] setupOnView:self.bottomeLabelV];
    self.scroreL = [[UILabel labelWithFrame:CGRectMake(self.fineMoneyL.right, self.fineMoneyL.top, self.fineMoneyL.width , self.fineMoneyL.height) font:self.scroreL.font text:@"" textColor:Label_Color] setupOnView:self.bottomeLabelV];
    
    
    [[[UIView viewWithFrame:CGRectMake(0, CELL_HEIGHT - 0.5, __MAIN_WIDTH, 0.5)] setupOnView:self] resetBackgroundColor:[Theme lineColor]];
}
- (void)reloadVioCarModel:(VioCarModel *)model
{
    [self.bottomWaningV setHidden:model.isVioInfoComplete];
    [self.bottomeLabelV setHidden:!self.bottomWaningV.hidden];
    
    if ([model.carinfoModel.modelImg isNotNil]) {
        [self.iconImageV setImageWithURL:[NSURL URLWithString:model.carinfoModel.modelImg] placeholderImage:ImageNamed(@"BRCarStyleNormalImage")];
        self.iconImageV.height = self.iconImageV.width * self.iconImageV.image.size.height / self.iconImageV.image.size.width;
        self.iconImageV.top = (CELL_HEIGHT - self.iconImageV.height) / 2;

    }
    
    NSString * brandNumStr = model.carinfoModel.carno;
    CGSize titleLSize = [brandNumStr sizeWithFont:self.titleL.font maxWidth:self.bottomeLabelV.width];
    self.titleL.text = [brandNumStr isNotNil] ? brandNumStr : @"车牌号待补充";
    self.titleL.textColor = [brandNumStr isNotNil] ? Label_Color : Label_Gray_Color;
    self.titleL.width = (titleLSize.width > 70 ? titleLSize.width : self.titleL.width) + 10;
    self.grayTitleL.left = self.titleL.right;
    [self.grayTitleL setHidden:!model.carinfoModel.unDel];
    
    self.warningDetailL.text = @"信息不完整";
    
    UIColor * leftColor = (model.vioCount > 0 || model.vioMoney > 0 || model.vioPoint > 0) ? Label_Color : Label_Gray_Color;
    UIColor * rightColor = (model.vioCount > 0 || model.vioMoney > 0 || model.vioPoint > 0) ? Label_Red_Color : Label_Color;
    
    [self.breakRulesL setAttributedText:[self attWithTitle:self.titleArray[0] numStr:[NSString stringWithFormat:@"%zd",model.vioCount] titleColor:leftColor numColor:rightColor]];
    [self.fineMoneyL setAttributedText:[self attWithTitle:self.titleArray[1] numStr:[NSString stringWithFormat:@"%.0f",model.vioMoney]titleColor:leftColor numColor:rightColor]];
    [self.scroreL setAttributedText:[self attWithTitle:self.titleArray[2] numStr:[NSString stringWithFormat:@"%zd",model.vioPoint]titleColor:leftColor numColor:rightColor]];
    

    
}

- (NSAttributedString *)attWithTitle:(NSString *)title numStr:(NSString *)numStr titleColor:(UIColor *)titleColor numColor:(UIColor *)numColor
{
    NSAttributedString * att1 = [[[NSAttributedString attributedStringWithText:title] copyAttributedStringWithFont:self.breakRulesL.font] copyAttributedStringWithColor:titleColor];
    NSAttributedString * att2 = [[[NSAttributedString attributedStringWithText:numStr] copyAttributedStringWithFont:self.breakRulesL.font] copyAttributedStringWithColor:numColor];
    return [NSAttributedString attributedStringWithAttributedStrings:@[att1,att2]];
}

- (NSArray *)titleArray
{
    return @[@"违章 ",@"罚款 ",@"扣分 "];
}
+ (CGFloat)getCellHeight
{
    return CELL_HEIGHT;
}

@end
